//
//  CRMyViewController.m
//  NewAge
//
//  Created by 王鑫 on 15/12/5.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CRMyViewController.h"
#import <MessageUI/MessageUI.h>

@interface CRMyViewController ()<UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIImageView *headImageView;

@end

@implementation CRMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"我的";
     [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.133f green:0.125f blue:0.125f alpha:1.00f];
    self.titleArray = @[@"我的收藏", @"我也不知打动", @"夜间模式", @"清理缓存", @"联系我们"];
    [self createTableView];
    [self createHeadImageView];

}

- (void)createTableView {
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = NO;
}

- (void)createHeadImageView {
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 5 * 2)];
    self.headImageView.backgroundColor = [UIColor redColor];
    self.myTableView.tableHeaderView = self.headImageView;
    self.headImageView.userInteractionEnabled = YES;  // 用来添加手势的方法
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
    
    if (indexPath.row == 0) {
        cell.textLabel.text = self.titleArray[0];
    } else if (indexPath.row == 1) {
        cell.textLabel.text = self.titleArray[1];
    } else if (indexPath.row == 2) {
        cell.textLabel.text = self.titleArray[2];
    } else if (indexPath.row == 3) {
        cell.textLabel.text = self.titleArray[3];
    } else if (indexPath.row == 4) {
        cell.textLabel.text = self.titleArray[4];
    } else {
        cell.textLabel.text = self.titleArray[5];
    }
    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 4) {
        Class mailClass = NSClassFromString(@"MFMailComposeViewController");
        if (mailClass) {
            if ([mailClass canSendMail]) {
                [self displayMailBox];
            } else {
                [self showMailBoxOnDevice];
            }
        } else {
            [self showMailBoxOnDevice];
        }
    }
}

- (void)showMailBoxOnDevice {
    NSString *mail = @"2293173615@qq.com";
    mail = [mail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mail]];
    
}

- (void)displayMailBox {
    MFMailComposeViewController *mailBox = [[MFMailComposeViewController alloc] init];
    NSArray *recipient = [NSArray arrayWithObjects:@"2563944534@qq.com", nil];
    [mailBox setToRecipients:recipient];
    // 填写主题
    [mailBox setSubject:@"意见反馈"];
    // 填写邮件内容
    NSString *messageBody = @"";
    [mailBox setMessageBody:messageBody isHTML:YES];
    
    // 设置代理人
    mailBox.mailComposeDelegate = self;
    
    [self presentViewController:mailBox animated:YES completion:^{
        
    }];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    NSString *message;
    switch (result) {
        case MFMailComposeResultCancelled:
            message = @"取消发送";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSaved:
            message = @"保存草稿";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSent:
            message = @"已发送";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultFailed:
            message = @"发送失败";
            NSLog(@"%@", message);
            break;
            
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
